package service.shiwu;

import dao.shiwu.ActorDao;
import domain.Actor;
import exception.BasicDaoException;

import java.util.List;

public class ActorService {
    private ActorDao actorDao = null;

    public boolean add(String name, String sex, String phone) throws BasicDaoException {

        int update = actorDao.update("insert into actor values(null,?,?,now(),?)", name, sex, phone);
        return update > 0;
    }

    public List<Actor> getList(int pageNo, String keyword) throws BasicDaoException {
        int limit = (pageNo - 1) * 5;
        return actorDao.getMulit("select * from actor where name like ? limit ?,5", Actor.class, "%" + keyword + "%",limit);
    }

    public Actor getInfo(int id) throws BasicDaoException {

        return actorDao.getSingle("select * from actor where id = ?", Actor.class, id);
    }

    public boolean update(int id, String name, String sex, String phone) throws BasicDaoException {
        int update = actorDao.update("update actor set name = ?, sex = ?, phone = ? where id = ?", name, sex, phone, id);
        return update > 0;
    }

    public boolean delete(int id) throws BasicDaoException {
        int update = actorDao.update("delete from actor where id = ?", id);
        return update > 0;
    }

    public int getTotal(String keyword) throws BasicDaoException {
        return actorDao.getTotal("select count(*) as total from actor where name like ?", "%" + keyword + "%");

    }
}
